package com.sky.loveshop.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.loveshop.entity.Shop;
import com.sky.loveshop.entity.qo.ShopQO;
import com.sky.loveshop.mapper.ShopMapper;
import com.sky.loveshop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:06
 */
@Slf4j
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public List<Shop> listShop(ShopQO shopQO) {
        log.info("listShopParam = {}", JSONUtil.toJsonStr(shopQO));
        return shopMapper.searchAll(shopQO);
    }

    @Override
    public PageInfo<Shop> listShopPages(ShopQO shopQO) {
        log.info("listShopPagesParam = {}", JSONUtil.toJsonStr(shopQO));
        PageHelper.startPage(shopQO.getPageNum(), shopQO.getPageSize());
        List<Shop> shops = shopMapper.searchAll(shopQO);
        return new PageInfo<>(shops);
    }

    @Override
    public Shop getShopById(Integer id) {
        return shopMapper.getShopById(id);
    }

    @Override
    public Shop getShopByStoreId(Integer storeId) {
        Example example = new Example(Shop.class);
        example.createCriteria().andEqualTo("storeId", storeId);
        return shopMapper.selectOneByExample(example);
    }

    @Override
    public List<Shop> getShopsByIds(List<Integer> ids) {
        return shopMapper.getShopsByIds(ids);
    }

    @Override
    public Integer addShop(Shop shop) {
        // 外送帮的商店id这里不维护，单独加一个接口，前端通过按钮进行外送帮绑定注册，然后返回store_id更新到对应的商店上。
        return shopMapper.insertOne(shop);
    }

    @Override
    public Integer delShopById(Integer id) {
        return shopMapper.delShopById(id);
    }

    @Override
    public Integer updateShopById(Shop shop) {
        return shopMapper.updateShopById(shop);
    }

    @Override
    public List<Shop> getAllShops() {
        return shopMapper.selectAll();
    }
}
